package com.zywl.utils.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class MsgUtils {
    protected static final Logger logger = Logger.getLogger(MsgUtils.class);

    public static String send(String mobile,String contetnt) throws Exception {
        URL url = new URL("https://gl.300c.cn/zyapi/sms/sendSms");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type",
                "application/json;charset=UTF-8");
        connection.connect();
        OutputStreamWriter out = new OutputStreamWriter(
                connection.getOutputStream(),"UTF-8");

        JSONObject objcontent = new JSONObject();
        objcontent.put("name",contetnt);

        JSONObject obj = new JSONObject();
        obj.put("mobile",mobile);
        obj.put("key", "LTAI5tQfUEYAKAwYzGhMyRwF");
        obj.put("key_secret", "uT4jIU11Ti8fBGME0i9CRDIpa5rJ3X");
        obj.put("model", "SMS_476700475");
        obj.put("sign", "浙江骁腾");
        obj.put("content", objcontent);
        out.write(obj.toJSONString());
        out.flush();
        out.close();
        String strLine = "";
        String strResponse = "";
        InputStream in = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
        while ((strLine = reader.readLine()) != null) {
            strResponse += strLine + "\n";
        }
        reader.close();
        connection.disconnect();
        return strResponse;
    }

    public static String sendCode(String mobile,String contetnt) throws Exception {
        URL url = new URL("https://gl.300c.cn/zyapi/sms/sendSms");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type",
                "application/json;charset=UTF-8");
        connection.connect();
        OutputStreamWriter out = new OutputStreamWriter(
                connection.getOutputStream(),"UTF-8");

        JSONObject objcontent = new JSONObject();
        objcontent.put("code",contetnt);

        JSONObject obj = new JSONObject();
        obj.put("mobile",mobile);
        obj.put("key", "LTAI5tQfUEYAKAwYzGhMyRwF");
        obj.put("key_secret", "uT4jIU11Ti8fBGME0i9CRDIpa5rJ3X");
        obj.put("model", "SMS_476700475");
        obj.put("sign", "浙江骁腾");
        obj.put("content", objcontent);
        out.write(obj.toJSONString());
        out.flush();
        out.close();
        String strLine = "";
        String strResponse = "";
        InputStream in = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
        while ((strLine = reader.readLine()) != null) {
            strResponse += strLine + "\n";
        }
        reader.close();
        connection.disconnect();
        return strResponse;
    }


    public static void main(String[] args) throws Exception {
//        MsgUtils.send("15088689998","测试一下");
//        newCaseNotify("15088689998","20181119110835434");
//        Date d = DateUtil.convertStringToDate("yyyy-MM-dd", "2018-11-19 00:25");
//        System.out.println(d);
//
//
//        JSONObject objcontent = new JSONObject();
//        objcontent.put("code","1234");
//
//
//        JSONObject obj = new JSONObject();
//        obj.put("mobile","13738079664");
//        obj.put("account_pass", "zytest20210630");
//        obj.put("temp_id", "SMS_151997147");
//        obj.put("content", objcontent);
//        obj.put("account", "zytest");
//        System.out.println(obj.toJSONString());

        System.out.println(sendCode("13738079664","云上智界"));
    }
}
